﻿using System.Collections.Generic;
using UnityEngine;

namespace GameEntry
{
    public abstract class UnitySerializedDictionary<TKey, TValue> : Dictionary<TKey, TValue>, ISerializationCallbackReceiver
    {
        [SerializeField, HideInInspector]
        private List<TKey> m_KeyData = new List<TKey>();
        
        [SerializeField, HideInInspector]
        private List<TValue> m_ValueData = new List<TValue>();

        void ISerializationCallbackReceiver.OnAfterDeserialize()
        {
            Clear();
            for (var i = 0; i < m_KeyData.Count && i < m_ValueData.Count; i++)
            {
                this[m_KeyData[i]] = m_ValueData[i];
            }
        }

        void ISerializationCallbackReceiver.OnBeforeSerialize()
        {
            m_KeyData.Clear();
            m_ValueData.Clear();

            foreach (var item in this)
            {
                m_KeyData.Add(item.Key);
                m_ValueData.Add(item.Value);
            }
        }
    }
}